/*
 * Created on 2005-1-25
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.karelun.gef.policies;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.jface.viewers.CellEditor;
import org.karelun.gef.commands.RenameNodeCommand;
import org.karelun.gef.figure.NodeFigure;
import org.karelun.gef.model.Node;

public class NodeDirectEditPolicy extends DirectEditPolicy{

    protected Command getDirectEditCommand(DirectEditRequest request) {
        RenameNodeCommand cmd = new RenameNodeCommand();
        Node node=(Node) getHost().getModel();
        cmd.setNode(node);
        CellEditor cellEditor = request.getCellEditor();
		Object value = cellEditor.getValue();
		cmd.setName((String) value);
        return cmd;
    }
    protected void showCurrentEditValue(DirectEditRequest request) {
        String value = (String) request.getCellEditor().getValue();
        IFigure figure = getHostFigure();
        if(figure instanceof Label)
        	((Label)figure).setText(value);
        else if(figure instanceof NodeFigure)
        ((NodeFigure) figure).setText(value);
    }
}
